package com.jinbei.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jinbei.common.Enum.CourseEnum;
import com.jinbei.common.base.BaseRedisService;
import com.jinbei.common.contants.Contants;
import com.jinbei.domain.Course;
import com.jinbei.mapper.CourseMapper;
import com.jinbei.mq.ActiveMqPublish;
import com.jinbei.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zflu
 * Created by lucky on 2018/8/7.
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ActiveMqPublish mqPublish;
    @Autowired
    private BaseRedisService redisService;

    /**
     * @author zflu
     * @Description   活动课程新增
     * @date 2018/8/7 16:21
     * @param course
     * @return int
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int insertCourse(Course course) throws Exception {
        int result = courseMapper.insertCourse(course);
        if(result > 0){
            course.setCommand(CourseEnum.Command.ADD);
            String message = JSONObject.toJSONString(course);
            //发布消息给mq
            mqPublish.publish(message);
            //将消息持久化到redis
            redisService.hset(Contants.COURSE_KEY,course.getType(),message);
        }
        return result;
    }

    /**
     * @author zflu
     * @Description   活动课程修改
     * @date 2018/8/7 16:22
     * @param course
     * @return int
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int updateCourse(Course course) throws Exception {
        int result = courseMapper.updateCourse(course);
        if(result > 0){
            course.setCommand(CourseEnum.Command.UPDATE);
            String message = JSONObject.toJSONString(course);
            //发布消息给mq
            mqPublish.publish(message);
            //将消息持久化到redis
            redisService.hset(Contants.COURSE_KEY,course.getType(),message);
        }
        return result;
    }

    /**
     * @author zflu
     * @Description   活动课程删除
     * @date 2018/8/7 16:23
     * @param type
     * @return int
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int deleteCourseByType(String type) throws Exception {
        int result = courseMapper.deleteCourseByType(type);
        if(result > 0){
            Course course = new Course();
            course.setCommand(CourseEnum.Command.DELETE);
            course.setType(type);
            String message = JSONObject.toJSONString(course);
            //发布消息给mq
            mqPublish.publish(message);
            //删除redis key
            redisService.hdel(Contants.COURSE_KEY,course.getType());
        }
        return result;
    }

    /**
     * @author zflu
     * @Description   课程活动查询
     * @date 2018/8/7 16:24
     * @param type
     * @return com.jinbei.domain.Course
     */
    @Override
    public Course queryCourseByType(String type) throws Exception {
        return courseMapper.queryCourseByType(type);
    }
}
